//
//  AdvertisingView.m
//  wedding
//
//  Created by 常宽 on 15/1/19.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "AdvertisingView.h"
#import "AutoScrollView.h"
#import "Advertising.h"

@interface AdvertisingView ()
@property (nonatomic, retain)AutoScrollView *autoView;  //自动滚动视图
@property (nonatomic, retain)NSMutableArray *imageArray;//图片URL字符串数字
@property (nonatomic, retain)NSMutableArray *dataArray; //数据数组
@end

@implementation AdvertisingView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //照片地址数组 初始化,开辟空间
        self.imageArray = [[NSMutableArray alloc]init];
        self.dataArray = [[NSMutableArray alloc] init];
        //网络请求
        [self netWorking];
    }
    return self;
}
- (void)netWorking
{
    NSString *string = @"http://data.halobear.cn/mapi/index.php?act=banner&cate=0&adcode=(null)";
    [NetHandler networkWithURL:string completion:^(id result) {
        NSArray *array = [result valueForKey:@"list"];
        for (NSDictionary *dic in array) {
            Advertising *model = [[Advertising alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            // url      加到数组里
            [self.imageArray addObject:model.banner_logo];
            // model    传到数组里
            [self.dataArray addObject:model];
        }
        [self.autoView setImageUrls:self.imageArray];
        //        NSLog(@"请求数据:%@",self.imageArray);
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.autoView = [[AutoScrollView alloc]initWithFrame:CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.height - 10)];
    self.autoView.showPageControl = NO;
    self.autoView.timeInterval = 5;
    [self.autoView setTarget:self action:@selector(photoShowAction:)];
    [self addSubview:_autoView];
}

- (void)photoShowAction:(AutoScrollView *)sender
{
    NSLog(@"点击第%d页", [sender pageNo]);
    Advertising *model = [self.dataArray objectAtIndex:[sender pageNo]];
    self.AdvBlock(model);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
